package com.jy.api.controller.web;

import com.jy.api.dao.OrderDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Order;
import com.jy.api.domain.Store;
import com.jy.api.vo.web.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lihu on 2017/12/5.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/web", produces = "application/json;charset=utf-8")
public class WebStoreController {

    @Autowired
    StoreDao storeDao;

    @Autowired
    OrderDao orderDao;


    /**
     * 获取店铺信息
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/store")
    public ResponseEntity queryStore(@RequestParam(name = "storeId") String storeId) {
        try {
            Store store = storeDao.findOne(storeId);
            if (store == null) {
                return new ResponseEntity("店铺信息不存在", HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity(store, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("queryStore is error==> storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 获取店铺信息
     *
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/store-info")
    public ResponseEntity queryOrder(@RequestParam(name = "orderId") String orderId) {
        try {
            Order order = orderDao.findOne(orderId);
            if (order == null) {
                return new ResponseEntity("授权失败！请重新扫码！", HttpStatus.BAD_REQUEST);
            }
            Store store = storeDao.findOne(order.getStoreId());
            if (store == null) {
                return new ResponseEntity("店铺信息不存在", HttpStatus.BAD_REQUEST);
            }
            StoreVo storeVo = new StoreVo();
            BeanUtils.copyProperties(store, storeVo);
            return new ResponseEntity(storeVo, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("queryOrder is error==> orderId:{}", orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
